from appium.webdriver.common.appiumby import AppiumBy
from base.base_page import find_and_do, press_keycode, switch_to_context, perform_touch_action, perform_swipe_sequence


class TestAndroid:
    # 初始化方法，接收driver参数
    def __init__(self, driver):
        self.driver = driver

    # 主要测试流程方法
    def test_android(self):
        driver = self.driver

        # 点击向导页面的下一步按钮，跳过引导页
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/ivNext")
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/ivNext")
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/ivNext")

        # 点击开始按钮，进入主应用
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/stv_start")

        # 输入邮箱，先输入错误邮箱再清除
        find_and_do(driver, 'send_keys', AppiumBy.ID, "joyhub.net.joyhub:id/etEmail", "ypcak@qq")
        find_and_do(driver, 'clear', AppiumBy.ID, "joyhub.net.joyhub:id/etEmail")

        # 输入验证码，先输入错误验证码再清除
        find_and_do(driver, 'send_keys', AppiumBy.ID, "joyhub.net.joyhub:id/etCode", "998877")
        find_and_do(driver, 'clear', AppiumBy.ID, "joyhub.net.joyhub:id/etCode")

        # 重新输入正确的邮箱和验证码
        find_and_do(driver, 'send_keys', AppiumBy.ID, "joyhub.net.joyhub:id/etEmail", "ypcna@qq.com")
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/etCode")
        find_and_do(driver, 'send_keys', AppiumBy.ID, "joyhub.net.joyhub:id/etCode", "998877")

        # 同意用户协议和隐私政策
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/cbAgreeOld")
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/cbAgreePrivate")

        # 点击登录/注册按钮
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/btnRegisterOrLogin")

        # 处理系统权限请求，允许应用在前台运行时访问权限
        find_and_do(driver, 'click', AppiumBy.ID,
                    "com.android.permissioncontroller:id/permission_allow_foreground_only_button")

        # 点击社区导航按钮
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/tv_community")

        # 同意社区协议
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/btn_agree")

        # 执行滑动操作，浏览社区内容
        swipes = [
            {'start_x': 514, 'start_y': 1969, 'end_x': 542, 'end_y': 532},
            {'start_x': 509, 'start_y': 2051, 'end_x': 542, 'end_y': 477}
        ]
        perform_swipe_sequence(driver, swipes)

        # 点击标题栏中的图片按钮
        find_and_do(driver, 'click', AppiumBy.XPATH,
                    "//android.view.ViewGroup[@resource-id='joyhub.net.joyhub:id/layout_title']/android.widget.ImageView[3]")

        # 点击第一个封面图片
        find_and_do(driver, 'click', AppiumBy.XPATH,
                    "(//android.widget.ImageView[@resource-id='joyhub.net.joyhub:id/img_cover'])[1]")

        # 点击评论输入框
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/et_comment")

        # 输入评论内容
        find_and_do(driver, 'send_keys', AppiumBy.ID, "joyhub.net.joyhub:id/et_comment_content", "test")

        # 点击发送评论按钮
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/iv_comment_sent")

        # 点击返回按钮，返回上一页
        find_and_do(driver, 'click', AppiumBy.ID, "joyhub.net.joyhub:id/fl_back")